package net.csdn.business.mirror;

import lombok.extern.slf4j.Slf4j;
import net.csdn.business.mirror.entity.bean.ScheduleTask;
import net.csdn.business.mirror.enums.ScheduleTaskEnum;
import net.csdn.business.mirror.service.ScheduleTaskService;
import net.csdn.business.mirror.utils.TimeUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;

import java.time.Duration;
import java.util.List;

@ActiveProfiles("dev")
@RunWith(SpringRunner.class)
@SpringBootTest(properties = {"spring.profiles.active=dev", "logging.file.path=/Users/yanlp/IdeaProjects/atomgit/openatom-mirror/logs"})
@Slf4j
public class ScheduleTaskServiceTest {

    @Autowired
    private ScheduleTaskService scheduleTaskService;

    @Test
    public void save() {
        ScheduleTask scheduleTask = ScheduleTask.builder()
                .title("镜像")
                .name("mirror")
                .cron("0 */1 * * * ?")
                .status(ScheduleTaskEnum.Status._1.code)
                .build();
        ScheduleTask r = scheduleTaskService.saveTask(scheduleTask);
        log.info("save result: {}", r);
    }

    @Test
    public void save2() {
        ScheduleTask scheduleTask = ScheduleTask.builder()
                .title("同步github数据")
                .name("sync_github")
                .cron("0 */5 * * * ?")
                .status(ScheduleTaskEnum.Status._1.code)
                .build();
        ScheduleTask r = scheduleTaskService.saveTask(scheduleTask);
        log.info("save result: {}", r);
        scheduleTaskService.rebuild();
    }

    @Test
    public void validList() {
        List<ScheduleTask> list = scheduleTaskService.validList();
        log.info("valid result: {}", list);
    }

    @Test
    public void getMinute() {
        String cron = "0 0 8 * * ?";
        Duration duration = TimeUtil.getCronDuration(cron);
        log.info("result: {}", duration.toMinutes());
    }
}
